﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Downloader.Domain;
using FluentNHibernate.Conventions;

namespace Downloader.Infrastructure
{
    public class PropertyConvention : IPropertyConvention
    {
        private static Type FieldLengthAttributeType = typeof(FieldLengthAttribute);
        //private static Type LazyLoadAttributeType = typeof(LazyLoadAttribute);
        #region IConvention<IPropertyInspector,IPropertyInstance> 成员

        public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance)
        {
            if (instance.Type.IsGenericType && instance.Type.GetGenericTypeDefinition() == typeof(FluentNHibernate.Mapping.GenericEnumMapper<>))
            {
                var realType = instance.Type.GenericArguments.First();
                if (realType.IsEnum)
                {
                    instance.CustomType(realType);
                }

            }

            var list = instance.Property.MemberInfo.GetCustomAttributes(FieldLengthAttributeType, true);
            if (list.Length > 0)
            {
                instance.Length(((FieldLengthAttribute)list[0]).Length);
            }
            //if (instance.Property.MemberInfo.GetCustomAttributes(LazyLoadAttributeType, true).Length > 0)
            //{
            //    instance.LazyLoad();
            //}
        }

        #endregion
    }
}
